package com.bobo.web.provider.util.commutils;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.env.Environment;

/**
 * Description
 * <p>
 *     用来获取指定的环境，判断当前环境是否为online
 * </p>
 * DATE 2017/11/14.
 *
 * @author liweijian.
 */

public class ProfileUtils {

    private static final Logger logger = LoggerFactory.getLogger(ProfileUtils.class);

    private static final String PROFILE;

    private static final String PROD = "prod";

    private static final String KEY = "spring.profiles.active";


    static {
        PROFILE = profile();
    }

    /**
     * Get current profile
     * @return current profile
     */
    private static String profile() {
        String profile = System.getProperty(KEY);
        if (StringUtils.isEmpty(profile)) {
            profile = System.getenv(KEY);
        }
        if (StringUtils.isEmpty(profile)) {
            Environment environment = SpringUtils.getBean(Environment.class);
            profile = JSON.toJSONString(environment.getActiveProfiles());
        }
        logger.info("-------------current profile---------------: {}", profile);
        return profile;
    }

    /**
     * Get current profile
     * @return current profile
     */
    public static String getProfile() {
        return PROFILE;
    }

    /**
     * Check if the profile is online
     * @return true if it is online or false
     */
    public static boolean isOnilne() {
        return ((JSONArray)JSONArray.parse(PROFILE)).contains(PROD);
    }
}
